import 'package:huxiaodui/api/api.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:huxiaodui/api/model/unit_model.dart';

class UnitInfoPage extends StatefulWidget{
  final UnitModel unit;
  const UnitInfoPage({super.key, required this.unit});

  @override
  State<StatefulWidget> createState() => UnitInfoPageState();


}


class UnitInfoPageState extends State<UnitInfoPage> {

  @override
  Widget build(BuildContext context) {


    Widget text(
        {required String labelText,
        required String text,
        required Icon icon,
        Function? onTap}) {
      var controller = TextEditingController(text: text);

      return TextField(
        textInputAction: TextInputAction.next,
        autofocus: true,
        readOnly: true,
        controller: controller,
        keyboardType: TextInputType.phone,
        decoration: InputDecoration(
          prefixIcon: icon,
          labelText: labelText,
        ),
        onTap: () {
          if (onTap != null) onTap();
        },
      );
    }

    return Scaffold(
      appBar: AppBar(
        title: const Text("单位"),
      ),
      body: Container(
        padding: const EdgeInsets.all(16),
        child: ListView(
          children: [
            const SizedBox(height: 10),
            text(
                labelText: "编号：",
                text: widget.unit.code,
                icon: const Icon(Icons.code)),
            const SizedBox(height: 10),
            text(
                labelText: "名字：",
                text: widget.unit.name,
                icon: const Icon(Icons.ad_units)),
            const SizedBox(height: 10),
            text(
                labelText: "地址：",
                text: widget.unit.addr,
                icon: const Icon(Icons.location_city)),
            const SizedBox(height: 10),
            text(
                labelText: "负责人：",
                text: widget.unit.manager,
                icon: const Icon(Icons.manage_accounts)),
            const SizedBox(height: 10),
            text(
                labelText: "电话：",
                text: widget.unit.tel,
                icon: const Icon(Icons.phone)),
            const SizedBox(height: 10),
            text(
                labelText: "派出所：",
                text: widget.unit.policeStation,
                icon: const Icon(Icons.local_police_outlined)),
            const SizedBox(height: 20),


            widget.unit.isJoin ?

            ElevatedButton(
              onPressed: () {
                Api.request(
                  url: UnitUrl.quit,
                  data: {"id": widget.unit.id},
                  success: (res) {
                    setState(() {
                      widget.unit.isJoin = false;
                    });
                  },
                  isToast: true,
                );
              },
              style: ButtonStyle(
                backgroundColor:
                MaterialStateProperty.all(Colors.red),
              ),
              child: Text("退出"),
            ):ElevatedButton(
              onPressed: () {
                Api.request(
                  url: UnitUrl.join,
                  data: {"id": widget.unit.id},
                  success: (res) {
                    setState(() {
                      widget.unit.isJoin = true;
                    });
                  },
                  isToast: true,
                );
              },
              child: Text("加入"),
            ),
          ],
        ),
      ),
    );
  }
}
